#include "ResourceLoaders/MeshLoader.h"
#include "FileUtils/TokenStream.h"
#include "FileUtils/Data.h"
using namespace utils;
namespace gengine
{
	namespace loaders
	{
		MeshLoader::MeshLoader(string name)
		{
			TokenStream tokenStream, tempStream;

	       std::string  token;
	       std::string  valtok;
	       std::string  buffer;
	       std::string tempLine;

		   utils::Data * data = utils::Data::getDataFromFile(name);
		   buffer = data->getString();

		   tokenStream.setTokenStream(buffer.c_str());

	

			tokenStream.resetStream();
	
			Vertex tempVertex;
			Vector3D tempVert;
			Vector3D tempNormals;
			Vector2D tempTexCoords;

			float x,y,z;
			float nx,ny,nz;
			float wx,wy;
			bool ready;
			int counter = 0;


	while(tokenStream.moveToNextLine(&tempLine))
	{
		tempStream.setTokenStream(tempLine.c_str());
		tokenStream.getNextToken(0);
		
		if(!tempStream.getNextToken(&token))
			continue;

		
		
		
		else if(tempLine == "#VertBegin" )
		{
		   		
			while((tokenStream.moveToNextLine(&tempLine))&&tempLine!="#VertEnd")
				{
		
					tempStream.setTokenStream(tempLine.c_str());
					
		
					tempStream.getNextToken(&token);
				   
					
					if(token == "v")
					{
						tempStream.getNextToken(&token);
						
						 x = (float)atof(token.c_str())/100;
						tempStream.getNextToken(&token);
						
						 y = (float)atof(token.c_str())/100;
						tempStream.getNextToken(&token);
						
						 z = (float)atof(token.c_str())/100;

						
					}
					
				
					if(token == "n")
					{
						tempStream.getNextToken(&token);
						
						 nx = (float)atof(token.c_str());
						tempStream.getNextToken(&token);
						
						 ny = (float)atof(token.c_str());
						tempStream.getNextToken(&token);
						
						 nz = (float)atof(token.c_str());
						
						
					}
					
					if(token == "t")
					{
						tempStream.getNextToken(&token);
						
						 wx = (float)atof(token.c_str());
						tempStream.getNextToken(&token);
						
						 wy = (float)atof(token.c_str());
						
						
						
					}
					  
					  counter ++;

					  if(counter == 3)
					  {
						  tempVertex.setPos(Vector3D(x,y,z));
						  tempVertex.setNormal(Vector3D(0,0,1));
					      tempVertex.setTexCoords(Vector2D(wx,wy));
					      streamVerts.push_back(tempVertex);
					      counter = 0;
						  
					  }
					
				}
			
		 
		}

	
        else  if(tempLine == "#IndexBegin" )
		{        
		  
			while((tokenStream.moveToNextLine(&tempLine))&&tempLine!="#IndexEnd")
				{
			        
		
					tempStream.setTokenStream(tempLine.c_str());
					
		
					tempStream.getNextToken(&token);
				   
					
					index.push_back(atoi(token.c_str()));
				}
			
		
		}

	}
	     delete data;
		}

	Vertex const*MeshLoader::GetVertexData() const
	{
		 return streamVerts.data();
	}
	int const*MeshLoader::GetIndexData()     const
	{
		return index.data();
	}

	}

	
}